package com.rambo.baselib.util;

import com.amap.api.maps.CoordinateConverter;
import com.amap.api.maps.model.LatLng;
import com.rambo.baselib.BaseApp;
import ohos.location.GeoAddress;
import ohos.location.GeoConvert;
import ohos.location.Location;

import java.io.IOException;
import java.util.List;

/***********************************************************************
 * <p>@description:
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/18 18:23
 **********************************************************************/
public class GpsUtil {
    /**


     * 逆 地理编码转化
     * @param location
     * @return
     */
    public static GeoAddress geoConvert(Location location) {
        try {
            List<GeoAddress> geoAddresses = new GeoConvert().getAddressFromLocation(location.getLatitude(), location.getLongitude(), 1);
            if (geoAddresses != null && geoAddresses.size() > 0) {
                return geoAddresses.get(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 转高德地图 坐标
     *
     * @param location
     * @return
     */
    public static Location toGdMapGps(Location location) {
        if (location == null) return null;
        // 定位返回的是GPS坐标，需要转换为高德坐标
        CoordinateConverter coordinateConverter = new CoordinateConverter(new com.amap.adapter.content.Context(BaseApp.getApp()));
        coordinateConverter.from(CoordinateConverter.CoordType.GPS);
        coordinateConverter.coord(new LatLng(location.getLatitude(), location.getLongitude()));
        LatLng latLng = coordinateConverter.convert();

        location.setLatitude(latLng.latitude);
        location.setLongitude(latLng.longitude);
        return location;

    }
}
